﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ooaavee.Support;
using System.Collections;
using System.Collections.ObjectModel;

namespace DevelopersTestPad.Worksheets.DotNet
{
    /// <summary>
    /// Displays object reflection data.
    /// </summary>
    internal partial class PushOutputSubView : SubView
    {
        public PushOutputSubView()
        {
            InitializeComponent();
            //InitializeTreeListView();
        }

        /// <summary>
        /// Adds a reflected object into the push output sub view.
        /// </summary>
        /// <param name="reflectedObject">The reflected object to add.</param>
        public void Add(Reflector.ReflectedObject reflectedObject)
        {
            reflectionTreeView.Add(reflectedObject);

            //ReflectionDataWrapperBase x = new ReflectedObjectDataWrapper() { Object = reflectedObject };

            
            //treeListView.AddObject(x);


            //treeListView.Expand(x);

            //int a = treeListView.GetItemCount();

            //treeListView.EnsureVisible(a-1);
        }

        ///// <summary>
        ///// Initializes the tree list view component.
        ///// </summary>
        //private void InitializeTreeListView()
        //{
        //    treeListView.CanExpandGetter = CanExpandGetter;
        //    treeListView.ChildrenGetter = ChildrenGetter;


        //    //this.treeListView.AddObject(new ReflectionDataWrapperBase() { Name = "Rivi 1", Type = "String", Value = "12345" } );
        //    //this.treeListView.AddObject(new ReflectionDataWrapperBase() { Name = "Rivi 2", Type = "Int32", Value = "12345" });
        //    //this.treeListView.AddObject(new ReflectionDataWrapperBase() { Name = "Rivi 3", Type = "DateTime", Value = "12345" });
            
        //}
        
        //private bool CanExpandGetter(object x)
        //{
        //    if (x is ReflectedObjectDataWrapper)
        //    {
        //        return true;
        //    }

        //    if (x is NonPublicMembersHeader)
        //    {
        //        return true;
        //    }

        //    if (x is ReflectedPropertyDataWrapper)
        //    {
        //        ReflectedPropertyDataWrapper model = (ReflectedPropertyDataWrapper)x;
        //        if (model.Property.PublicFields.Count > 0 || 
        //            model.Property.PublicProperties.Count > 0 ||
        //            model.Property.NonPublicFields.Count > 0 || 
        //            model.Property.NonPublicProperties.Count > 0)
        //        {
        //            return true;
        //        }
        //    }

        //    if (x is ReflectedFieldDataWrapper)
        //    {
        //        ReflectedFieldDataWrapper model = (ReflectedFieldDataWrapper)x;
        //        if (model.Field.PublicFields.Count > 0 ||
        //            model.Field.PublicProperties.Count > 0 ||
        //            model.Field.NonPublicFields.Count > 0 ||
        //            model.Field.NonPublicProperties.Count > 0)
        //        {
        //            return true;
        //        }
        //    }

        //    return false;
        //}

        //private IEnumerable ChildrenGetter(object x)
        //{
        //    ArrayList childrens = new ArrayList();

        //    if (x is ReflectedObjectDataWrapper)
        //    {
        //        ReflectedObjectDataWrapper model = x as ReflectedObjectDataWrapper;

        //        childrens.AddRange(GetChildrens(model.Object.PublicFields, model.Object.PublicProperties));

        //        if (model.Object.NonPublicFields.Count > 0 || model.Object.NonPublicProperties.Count > 0)
        //        {
        //            childrens.Add(new NonPublicMembersHeader { Object = model.Object });
        //        }
        //    }

        //    if (x is NonPublicMembersHeader)
        //    {
        //        NonPublicMembersHeader model = x as NonPublicMembersHeader;

        //        if (model.Object != null)
        //        {
        //            childrens.AddRange(GetChildrens(model.Object.NonPublicFields, model.Object.NonPublicProperties));                    
        //        }
        //        if (model.Property != null)
        //        {
        //            childrens.AddRange(GetChildrens(model.Property.NonPublicFields, model.Property.NonPublicProperties));
        //        }
        //        if (model.Field != null)
        //        {
        //            childrens.AddRange(GetChildrens(model.Field.NonPublicFields, model.Field.NonPublicProperties));
        //        }
        //    }

        //    if (x is ReflectedPropertyDataWrapper)
        //    {
        //        ReflectedPropertyDataWrapper model = x as ReflectedPropertyDataWrapper;

        //        childrens.AddRange(GetChildrens(model.Property.PublicFields, model.Property.PublicProperties));

        //        if (model.Property.NonPublicFields.Count > 0 || model.Property.NonPublicProperties.Count > 0)
        //        {
        //            childrens.Add(new NonPublicMembersHeader { Property = model.Property });
        //        }
        //    }

        //    if (x is ReflectedFieldDataWrapper)
        //    {
        //        ReflectedFieldDataWrapper model = x as ReflectedFieldDataWrapper;

        //        childrens.AddRange(GetChildrens(model.Field.PublicFields, model.Field.PublicProperties));

        //        if (model.Field.NonPublicFields.Count > 0 || model.Field.NonPublicProperties.Count > 0)
        //        {
        //            childrens.Add(new NonPublicMembersHeader { Field = model.Field });
        //        }
        //    }

        //    return childrens;            
        //}

        //private static ArrayList GetChildrens(Collection<Reflector.ReflectedField> fields, Collection<Reflector.ReflectedProperty> properties)
        //{
        //    ArrayList childrens = new ArrayList();

        //    foreach (Reflector.ReflectedField field in Sort(fields))
        //    {
        //        childrens.Add(new ReflectedFieldDataWrapper { Field = field });
        //    }

        //    foreach (Reflector.ReflectedProperty property in Sort(properties))
        //    {
        //        childrens.Add(new ReflectedPropertyDataWrapper { Property = property });
        //    }

        //    return childrens;
        //}

        //private static IEnumerable Sort(Collection<Reflector.ReflectedField> fields)
        //{
        //    List<Reflector.ReflectedField> f = new List<Reflector.ReflectedField>(fields);
        //    f.Sort((f1, f2) => String.Compare(f1.Name, f2.Name, StringComparison.CurrentCulture));
        //    return f;
        //}

        //private static IEnumerable Sort(Collection<Reflector.ReflectedProperty> properties)
        //{
        //    List<Reflector.ReflectedProperty> p = new List<Reflector.ReflectedProperty>(properties);
        //    p.Sort((p1, p2) => String.Compare(p1.Name, p2.Name, StringComparison.CurrentCulture));
        //    return p;
        //}

        //private abstract class ReflectionDataWrapperBase
        //{
        //    public abstract string Name { get; }
        //    public abstract string Value { get; }
        //    public abstract string Type { get; }
        //}

        //private sealed class ReflectedObjectDataWrapper : ReflectionDataWrapperBase
        //{
        //    public Reflector.ReflectedObject Object { set; get; }

        //    public override string Name
        //    {
        //        get { return "{" + Object.Type.FullName + "}"; }
        //    }

        //    public override string Value
        //    {
        //        get { return Object.Value; }
        //    }

        //    public override string Type
        //    {
        //        get { return Object.Type.FullName; }
        //    }
        //}

        //private sealed class NonPublicMembersHeader : ReflectionDataWrapperBase
        //{
        //    public Reflector.ReflectedObject Object { set; get; }
        //    public Reflector.ReflectedProperty Property { set; get; }
        //    public Reflector.ReflectedField Field { set; get; }

        //    public override string Name
        //    {
        //        get { return "Non-Public members"; }
        //    }

        //    public override string Value
        //    {
        //        get { return string.Empty; }
        //    }

        //    public override string Type
        //    {
        //        get { return string.Empty; }
        //    }
        //}

        //private sealed class ReflectedPropertyDataWrapper : ReflectionDataWrapperBase
        //{
        //    public Reflector.ReflectedProperty Property { set; get; }

        //    public override string Name
        //    {
        //        get { return Property.Name; }
        //    }

        //    public override string Value
        //    {
        //        get { return Property.Value; }
        //    }

        //    public override string Type
        //    {
        //        get { return Property.Type.FullName; }
        //    }
        //}

        //private sealed class ReflectedFieldDataWrapper : ReflectionDataWrapperBase
        //{
        //    public Reflector.ReflectedField Field { set; get; }

        //    public override string Name
        //    {
        //        get { return Field.Name; }
        //    }

        //    public override string Value
        //    {
        //        get { return Field.Value; }
        //    }

        //    public override string Type
        //    {
        //        get { return Field.Type.FullName; }
        //    }
        //}

    }
}
